﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.IO;
using HYI.SDBSERVER;

namespace mySUI
{
    public partial class FMLogin : Form
    {
        DBSERVER myADO = new DBSERVER();

        public FMLogin()
        {
            InitializeComponent();
        }

        //确定按钮事件
        private void uibtn1_Click(object sender, EventArgs e)
        {
            if (!uiuserno.Text.IsValid())
            {
                this.ShowWarningDialog("用户编号不能为空");
                uiuserno.Focus();
            }

            if (!uipassword.Text.IsValid())
            {
                this.ShowWarningDialog("密码不能为空");
                uipassword.Focus();
            }
            
            string sql = "select username,password,status,fsite_no_belong,b.role_id "
                + " from am_sys_user a"
                + " left join am_sys_user_role b on b.user_id=a.id"
                + " where id="+uiuserno.Text;
            DataTable myTable = myADO.query(sql);

            if ((myTable ==null) || (myTable.Rows.Count == 0)){
                this.ShowWarningDialog("用户编号不正确");
                return;
            }

            if (myTable.Rows[0]["status"].ToString() == "0")
            {
                this.ShowWarningDialog("该用户禁止登录");
                return;
            }

            if (!myTable.Rows[0]["password"].ToString().Equals(uipassword.Text))
            {
                this.ShowWarningDialog("密码不正确");
                return;
            }

            string sUserNo = uiuserno.Text;
            string sUserName = myTable.Rows[0]["username"].ToString();
            string sSiteNo = myTable.Rows[0]["fsite_no_belong"].ToString();
            string srole_id = myTable.Rows[0]["role_id"].ToString();

            //保存当前登陆信息
            myADO.doSetLogin(sUserNo, uipassword.Text);
            myADO.DeleteDir("log");
            //myADO.Dispose();

            Form1 m = new Form1();
            m.myADO = myADO;
            m.sUserNo = sUserNo;
            m.sUserName = sUserName;
            m.sSiteNo = sSiteNo;
            m.srole_id = srole_id;
            m.Show();

            this.Visible = false;
        }

        private void uibtn2_Click(object sender, EventArgs e)
        {
            if (this.ShowAskDialog("确定退出吗?"))
            {
                myADO.Dispose();

                Application.Exit();
            }
        }

        private void FMLogin_Load(object sender, EventArgs e)
        {
            //Logo图片
            string str = System.Environment.CurrentDirectory + "\\image\\logo.jpg";
            if (File.Exists(str))
            {
                try
                {
                    this.pictureBox1.Image = Image.FromFile(str);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                str = System.Environment.CurrentDirectory + "\\image\\logo.jpeg";
                if (File.Exists(str))
                {
                    try
                    {
                        this.pictureBox1.Image = Image.FromFile(str);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }

            string sVersion = "(版本:" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + ")";
            uiCompany.Text = "合极单机版物料管理柜软件" + sVersion;
            string sMsg = myADO.doGetLogin();
            string[] strArray = sMsg.Split(';');
            uiuserno.Text = strArray[0];
            uipassword.Text = strArray[1];
        }
    }
}
